<template>
	<view>
		<uni-easyinput v-model="condition" placeholder="请输入搜索条件(如id:3,title:t    以分号和逗号进行切割)"></uni-easyinput><button
			@click="search()">搜索</button>
		<!-- <my-section v-for="(item,index) in section" :key="index" :begin="item.begin" :end="item.end"></my-section> -->
		<my-item v-for="(item) in list" :id="item.id" :title="item.title" :detail="item.content"></my-item>
		<button class="create" @click="create()">发帖</button>
	</view>

</template>

<script>
	import myItem from '../../components/my-item.vue'
	// import {
	// 	list
	// } from './list.js'
	export default {
		components: {
			myItem
		},
		data() {
			return {
				condition: "",
				list: []
			}
		},
		onLoad() {

		},
		onReady() {},
		onPullDownRefresh() {

		},
		onReachBottom() {

		},
		onShow() {
			this.clicked()
		},
		methods: {
			clicked() {
				this.list = []
				uni.showLoading({
					title: '加载中',
				})
				uni.request({
					url: this.$baseUrl + '/positing/get',
					method: 'GET',
					header: {
						'Content-Type': 'application/json;charset=UTF-8',
						'token': uni.getStorageSync("token")
					},
					success: (res) => {
						if (res.data.code === "0000") {
							console.log("list length " + this.list.length)
							for (var key in res.data.data.posting_list) {
								var postings = res.data.data.posting_list[key]
								this.list.push(postings)
							}
						} else if (res.data.code === "4000") {
							alert("请重新登录")
							uni.switchTab({
								url:'../logic/login'
							})
						} else {
							alert(res.data.message)
						}
						uni.hideLoading()
					}
				})
			},
			search() {
				this.list = []
				var conditionData = {}
				var condition = this.condition
				var conList = condition.split(",")
				for (var i = 0; i < conList.length; i++) {
					var sigleCondition = conList[i]
					var sigleList = sigleCondition.split(":")
					if (sigleList.length == 1) {
						this.clicked()
						return
					}
					if (sigleList.length != 2) {
						alert("请输入正确的搜索条件")
						this.clicked()
						return
					}
					if (sigleList[0] === "id") {
						conditionData["id"] = parseInt(sigleList[1])
					}
					if (sigleList[0] === "title") {
						conditionData["title"] = sigleList[1]
					}
					if (sigleList[0] === "content") {
						conditionData["content"] = sigleList[1]
					}
				}
				uni.request({
					url: this.$baseUrl + '/positing/get/condition',
					method: 'POST',
					header: {
						'Content-Type': 'application/json;charset=UTF-8',
						'token': uni.getStorageSync("token")
					},
					data: conditionData,
					success: (res) => {
						if (res.data.code === "0000") {
							for (var key in res.data.data.posting_list) {
								var postings = res.data.data.posting_list[key]
								this.list.push(postings)
							}
						} else if (res.data.code === "4000") {
							alert("请重新登录")
							uni.switchTab({
								url:'../logic/login'
							})
						} else {
							alert(res.data.message)
						}
						uni.hideLoading()
					}
				})
			},
			create() {
				uni.navigateTo({
					url: "edit?title=&content=&id=-1"
				})
			}
		},

	}
</script>

<style>
	.create {
		position: fixed;
		width: 100%;
		bottom: var(--window-bottom, 0);
		background-color: #18BC37;
	}
</style>
